package com.loop.stringBuilder;

public class StringBuilderDemo {
    public static void main(String[] args) {
        //无参构造创建对象
        StringBuilder s1 = new StringBuilder();
        //一参构造，规定初始容量的大小
        StringBuilder s2 = new StringBuilder(20);
        //一参构造，增入一个字符串，而后此容量为字符串的长度加上16
        StringBuilder s3 = new StringBuilder("hello");

        //StringBuilder类已经重写了toSting方法为打印字符
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        //capacity()表示StringBuilder的容量大小
        System.out.println(s1.capacity());
        System.out.println(s2.capacity());
        System.out.println(s3.capacity());

        //length()方法表示的是实际有效字符的长度
        System.out.println(s1.length());
        System.out.println(s2.length());
        System.out.println(s3.length());

        function();
    }

    /**
     * StringBuilder的方法
     */
    public static void function(){
        StringBuilder s = new StringBuilder("abc");
        //append和insert方法
        s.insert(s.length(),"哈哈哈");//在指定下标插入字符串内容
        System.out.println(s);
        s.append(1);
        s.append(true);
        System.out.println(s);

        StringBuilder s2 = new StringBuilder("HelloWorld");
        s2.deleteCharAt(6);//删除指定位置的字符
        s2.delete(1,5);//删除指定区域的字符串内容
        System.out.println(s2);

        s2.replace(1,2,"place");//指定区域的字符串替换
        System.out.println(s2);
        s2.reverse();//字符串反转
        System.out.println(s2);
    }
}
